﻿/************************************************************************

   Extended Silverlight Toolkit – SAMPLE CODE

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

 ************************************************************************

  *** IMPORTANT NETFLIX INFORMATION *** 

   This sample application accesses data from the Netflix's OData Catalog API. 
   Before you compile this sample application, you must agree to the terms and conditions 
   of the Netflix API Terms of Use agreement. 

   The agreement is available here: http://developer.netflix.com/page/Api_terms_of_use

  ***********************************************************************/

using System;

#if SILVERLIGHT
using System.Windows.Browser;
#else
using System.Diagnostics;
#endif
using System.Windows.Controls;
using Xceed.Silverlight.Data;
using Xceed.Utils.Silverlight.LiveExplorer;

namespace Xceed.Silverlight.DataGrid.Samples
{
  public partial class RemoteDataAccessSample : UserControl, ISampleHost, ISampleError
  {
    public RemoteDataAccessSample()
    {
      NetflixCatalog.NetflixCatalog catalog = new NetflixCatalog.NetflixCatalog( new Uri( "http://odata.netflix.com/Catalog/", UriKind.Absolute ) );

      this.DataContext = new WcfDataServicesDataSourceProvider<NetflixCatalog.Title>( catalog.Titles.IncludeTotalCount(), catalog );

      InitializeComponent();
    }

    private void OnDataGridUnhandledDataException( object sender, UnhandledDataExceptionEventArgs e )
    {
      if( !e.Handled )
      {
        e.Handled = true;

        if( this.Error != null )
        {
          this.Error( this, new SampleErrorEventArgs( "The WebService returned an exception that forced the sample to reload. Original message: " + e.Exception.Message, e.Exception ) );
        }

        e.Result = ErrorResultAction.Abort;
      }
    }

    //  This function is necessary to manage button click. The button
    //  is useful because Hyperlink doesn't work the same way in Silverlight vs WPF.
    private void HyperlinkButton_Click( object sender, EventArgs e )
    {
      Button button = ( Button )sender;
      string buttonLink = ( string )button.DataContext;

#if SILVERLIGHT
      HtmlPage.PopupWindow( new Uri( buttonLink, UriKind.Absolute ), "_blank", null );
#else
      ProcessStartInfo startInfo = new ProcessStartInfo( buttonLink );
      Process.Start( startInfo );
#endif
    }

    #region ISampleHost Members

    public object SampleHost
    {
      get
      {
        return this;
      }
    }

    #endregion // ISampleHost Members

    #region ISampleError Members

    public event EventHandler<SampleErrorEventArgs> Error;

    #endregion
  }
}
